package com.shop.controller;

import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.shop.pojo.ProductInfo;
import com.shop.pojo.vo.ProductInfoVo;
import com.shop.service.ProductInfoService;
import com.shop.utils.FileNameUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/prod")
public class ProductInfoAction {


    @Autowired
    private ProductInfoService productInfoService;
    //    文件的名称
    private  String savaFileName ="";
    //    分页的总页数
    private static Integer pageSize = 5;

    //    获得所有的商品
    @RequestMapping("/all.action")
    public String findAll(HttpServletRequest request) {
        request.setAttribute("list", this.productInfoService.findAll());
        return "product";
    }
    //    分页的操作的
    @RequestMapping("/split.action")
    public String split(HttpServletRequest request) {
        PageInfo<ProductInfo> productInfoPageInfo = null;
//首先是第一页的展示
        Object page = request.getAttribute("page");
        if (page != null) {
//            说明是其他页面跳转过来的
            productInfoPageInfo =    this.productInfoService.sliptPage(Integer.parseInt((String) page), pageSize);
        } else {
            productInfoPageInfo =    this.productInfoService.sliptPage(1, pageSize);
        }

        System.out.println("----------->" +productInfoPageInfo);
//        存储数据
        request.getSession().setAttribute("info",productInfoPageInfo);
        return "product";
    }
    //    异步ajaxSplit
//    分页的操作的
    @RequestMapping(value = "/ajaxSplit.action",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String ajaxSplit(ProductInfoVo productInfoVo, HttpServletRequest request) {
//首先是第一页的展示
        PageInfo<ProductInfo> productInfoPageInfo = this.productInfoService.sliptPage(productInfoVo, pageSize);
//        存储数据session中去
        request.getSession().setAttribute("info",productInfoPageInfo);
        return (String) request.getAttribute("msg");
    }
//异步的添加图片，并且回显图片

    @RequestMapping("/ajaxImg.action")
    @ResponseBody
    public String ajaxImg(MultipartFile pimage, HttpServletRequest request) {
        this.savaFileName = FileNameUtil.getUUIDFileName() + FileNameUtil.getFileType(pimage.getOriginalFilename());
        //        得到项目中的路径
        String path = request.getServletContext().getRealPath("image_big");
        System.out.println("图片上传的地址--------->" +path);
        //        转存
        File file = new File(path + File.separator + this.savaFileName);


        try {
//            写入文件
            pimage.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String,String> map = new HashMap<>();
        map.put("imgUrl",this.savaFileName);

        String jsonStr = JSONUtil.toJsonStr(map);
        return jsonStr;
    }

    @RequestMapping("save.action")
    public String save(ProductInfo productInfo, HttpServletRequest request) {
        productInfo.setpDate(new Date());
        productInfo.setpImage(this.savaFileName);
        Integer integer = this.productInfoService.addProduct(productInfo);
        if (integer > 0) {
//            虽然是转发过去的，但是页面上的数据是通过load加载的，所以request无效
            request.getSession().setAttribute("msg","添加成功");
        } else {
            request.getSession().setAttribute("msg","添加失败");
        }
//        添加商品之后，清除文件名字
        this.savaFileName = "";
//        增加商品完之后，我回到第一页
        return "redirect:/prod/split.action";
    }
    //    删除商品,回到删除商品的页面出
    @RequestMapping("deleteOne.action")
    public String removeProduct(Integer pid, HttpServletRequest request) {
        try {
            Integer integer = this.productInfoService.removeProduct(pid);
            if (integer > 0) {
                request.setAttribute("msg","删除成功");
            } else {
                request.setAttribute("msg","删除失败");

            }

        } catch (Exception e) {
            request.setAttribute("msg","该商品不能删除");
        }

        return "forward:/prod/ajaxSplit.action";

    }

    // 异步分页的操作的
    @RequestMapping(value = "/deleteSplit.action", produces = "text/html;charset=utf-8")
    @ResponseBody
    public String deleteSplit(ProductInfoVo productInfoVo,HttpServletRequest request) {
        productInfoVo.setPage(1);
        PageInfo<ProductInfo> productInfoPageInfo = this.productInfoService.sliptPage(productInfoVo, pageSize);
        System.out.println("----------->" +productInfoPageInfo);
//        存储数据
        request.getSession().setAttribute("info",productInfoPageInfo);
        return (String) request.getAttribute("msg");
    }

    //    批量删除
    @RequestMapping("/deleteBatch.action")
    public String removeBatchProduct(String pid, HttpServletRequest request) {
        System.out.println(pid);
//调用业务逻辑层
        this.productInfoService.removeBatchProduct(pid.split(","));
        request.setAttribute("msg","批量删除成功");
//        让它回到第一页
        return "forward:/prod/deleteSplit.action";

    }

    //    先查找 需要修改的商品
    @RequestMapping("/selectById.action")
    public String selectById(String pid, String page, HttpServletRequest request) {
        ProductInfo productInfo = this.productInfoService.findById(Integer.parseInt(pid));
        request.setAttribute("prod",productInfo);
        request.setAttribute("page",page);

        return "update";
    }
    //    修改
    @RequestMapping("/update.action")
    public String updateProduct(ProductInfo productInfo,HttpServletRequest request) {
        productInfo.setpDate(new Date());
        if (this.savaFileName.length()>0) {
            productInfo.setpImage(this.savaFileName);
        }

        System.out.println(productInfo + "prod");
        Integer integer = this.productInfoService.modifyProduct(productInfo);
        if (integer > 0 && this.savaFileName.length() > 0) {
            request.getSession().setAttribute("msg","修改成功");
//            清楚文件名称
            this.savaFileName = "";
        } else {
            request.getSession().setAttribute("msg","修改失败");
        }
        return "forward:/prod/split.action";
    }

}
